<template>
  <div>【竖向柱状图】</div>
  <div ref="target" class="w-full flex-1"></div>
</template>
<script setup>
import { ref, onMounted, watch } from 'vue';
import * as echarts from 'echarts';
const props = defineProps({
  data: {
    type: Object,
    required: true,
  },
});

watch(
  () => props.data,
  () => {
    renderChart();
  }
);

let myChart = ref(null);
const target = ref(null);

const renderChart = () => {
  const options = {
    xAxis: {
      type: 'category',
      data: props.data.map((item) => item.name),
      axisLabel: {
        color: '#fff',
      },
      // inverse: true,
    },
    yAxis: {
      type: 'value',
      show: false,
      max: function (value) {
        return parseInt(value.max * 1.1);
      },
    },
    grid: {
      top: 16,
      right: 0,
      bottom: 26,
      left: -26,
      containLabel: true,
    },
    series: [
      {
        type: 'bar',
        data: props.data.map((item) => ({
          value: item.value,
        })),
        itemStyle: {
          color: '#5d98ce',
          barBorderRadius: 5,
          shadowColor: 'rgba(0, 0, 0, 0.3)',
          shadowBlur: 5,
        },
        barWidth: 12,
        label: {
          show: true,
          position: 'top',
          textStyle: {
            color: '#fff',
          },
          formatter: '{c}%',
        },
      },
    ],
  };

  myChart.setOption(options);
};

onMounted(() => {
  myChart = echarts.init(target.value);
  renderChart();
});
</script>
<style lang="scss" scoped></style>
